// bad cat!
#include <bits/stdc++.h>
using namespace std;

int n, m, t, a[131072], c[131072], D[18][131072], x[4], val[262144], lim[262144], son[262144], leaf[131072];
long long sum[262144], ans[131072];

inline void up(int d, int o) {
	if (val[son[o]] == -1) {
		val[o] = -1;
		sum[o] = sum[o << 1] + sum[o << 1 | 1];
	} else if (val[son[o]] >= d) {
		val[o] = val[son[o]];
		sum[o] = sum[son[o]];
	} else {
		val[o] = val[son[o] ^ 1];
		sum[o] = sum[son[o] ^ 1];
	}
}

void build(int d, int l, int r, int o, int flg) {
	if (l == r) {
		if (flg) {
			val[o] = a[l];
			sum[o] = (a[l] >= lim[o] ? l + 1 : 0);
		} else {
			val[o] = -1;
			sum[o] = l + 1;
			leaf[l] = o;
		}
		return;
	}
	int mid = (l + r) / 2;
	son[o] = (o << 1 | D[d][l >> d]);
	lim[son[o]] = max(lim[o], d);
	lim[son[o] ^ 1] = lim[o];
	build(d - 1, l, mid, o << 1, o == 1 ? 1 : flg);
	build(d - 1, mid + 1, r, o << 1 | 1, o == 1 ? 0 : flg);
	up(d, o);
}

void modify(int x, int y) {
	int o = leaf[x], d = 1;
	val[o] = y;
	sum[o] = (y >= lim[o] ? x + 1 : 0);
	o >>= 1;
	while (o) {
		int v = val[o], s = sum[o];
		up(d, o);
		if (v == val[o] && s == sum[o]) {
			return;
		}
		o >>= 1;
		d++;
	}
}

signed main() {
	freopen("arena.in", "r", stdin);
	freopen("arena.out", "w", stdout);
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	for (int i = 0; i < m; i++) {
		scanf("%d", &c[i]);
	}
	int k = __lg(n * 2 - 1);
	for (int i = 1; i <= k; i++) {
		for (int j = 0; j < (1 << (k - i)); j++) {
			scanf("%1d", &D[i][j]);
		}
	}
	scanf("%d", &t);
	while (t--) {
		for (int i = 0; i < 4; i++) {
			scanf("%d", &x[i]);
		}
		for (int i = 0; i < n; i++) {
			a[i] ^= x[(i + 1) % 4];
		}
		for (int i = k; i >= 0; i--) {
			build(i, 0, (1 << i) - 1, 1, 0);
			for (int j = (i == 0 ? 0 : (1 << (i - 1))); j < (1 << i) && j < n; j++) {
				modify(j, a[j]);
				ans[j + 1] = sum[1];
			}
		}
		long long res = 0;
		for (int i = 0; i < m; i++) {
			res ^= (ans[c[i]] * (i + 1));
		}
		printf("%lld\n", res);
		for (int i = 0; i < n; i++) {
			a[i] ^= x[(i + 1) % 4];
		}
	}
}
